<template>
  <div>
<!--    面包屑导航区域-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
<!--    卡片视图区域-->
    <el-card>
<!--      搜索商品区域-->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable @clear="getGoodsList" >
            <el-button slot="append"  icon="el-icon-search"  @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
<!--      添加商品按钮-->
      <el-col :span="4">
      <el-button type="primary" @click="goAddPage">添加商品</el-button>
      </el-col>
      </el-row>
<!--      商品列表区域-->
      <el-table :data="goodsList" border stripe :header-cell-style="{background:'#eef1f6',color:'#606266'}">
        <el-table-column type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name" width="505px"></el-table-column>
        <el-table-column label="商品价格" prop="goods_price"></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight"></el-table-column>
        <el-table-column label="创建事件" prop="dateFormat(add_time)"><template slot-scope="scope">{{scope.row.add_time | dateFormat}}</template></el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog(scope.row.goods_id)"></el-button>
            <!-- 删除按钮 -->
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeGoodById(scope.row.goods_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 30, 50]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
<!--    修改商品信息对话框-->
    <el-dialog title="修改商品" :visible.sync="showEditDialogVisible" width="50%" @close="editDialogClosed">
      <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="70px">
        <el-form-item label="商品名" prop="goods_name">
          <el-input v-model="editForm.goods_name" disabled></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="goods_price">
          <el-input v-model="editForm.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="商品重量" prop="goods_weight">
        <el-input v-model="editForm.goods_weight"></el-input>
      </el-form-item>
        <el-form-item label="商品介绍" prop="goods_introduce">
          <el-input v-model="editForm.goods_introduce"></el-input>
        </el-form-item>
        <el-form-item label="商品图片" prop="pics">
          <el-input v-model="editForm.pics"></el-input>
        </el-form-item>
        <el-form-item label="商品参数" prop="attrs">
          <el-input v-model="editForm.attrs"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showEditDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editGoodInfo">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  //  自定义数据区域
  data () {
    return {
      //  是否展示添加商品对话框
      showAddGoodsDialog: false,
      //  是否展示编辑商品的对话框
      showEditDialogVisible: false,
      //  修改商品绑定的数据
      editForm: {},
      //  商品列表
      goodsList: [],
      total: 0,
      queryInfo: {
        query: '',
        //  当前页数
        pagenum: 1,
        //  当前每页展示多少条数据
        pagesize: 6
      },
      editFormRules: {
        goods_name: [
          { required: true,
            message: '请输入商品名',
            trigger: 'blur'
          },
          {
            min: 2,
            max: 10,
            message: '商品名的长度在2~10个字符之间',
            trigger: 'blur'
          }
        ],
        goods_price: [
          { required: true, message: '请输入价格', trigger: 'blur' },
          { min: 1,
            max: 10,
            message: '价格的长度在1~10个字符之间',
            trigger: 'blur'
          }
        ],
        goods_weight: [
          { required: true,
            message: '请输入商品重量',
            trigger: 'blur'
          },
          { min: 1,
            max: 10,
            message: '重量的长度在1~10个字符之间',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  //  默认函数
  created() {
    this.getGoodsList()
  },
  //  自定义方法区域
  methods: {
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表失败！')
      }
      console.log(res.data)
      this.goodsList = res.data.goods
      this.total = res.data.total
      this.$message.success('获取商品列表成功！')
    },
    //  跳转到添加页面
    goAddPage() {
      this.$router.push('/goods/add')
    },
    //  编辑对话框关闭清空表单数据
    editDialogClosed () {
      this.showEditDialogVisible = false
      this.$refs.editFormRef.resetFields()
    },
    //  根据id展示编辑对话框
    async showEditDialog (id) {
      console.log(id)
      this.showEditDialogVisible = true
      const { data: res } = await this.$http.get(`goods/${id}`)
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品信息失败！')
      }
      console.log(res.data)
      this.editForm = res.data
      this.$message.success('获取商品信息成功！')
    },
    //  保存修改后的商品信息
    async  editGoodInfo () {
      const { data: res } = await this.$http.put(`goods/${this.id}`, { params: this.editForm })
      if (res.meta.data !== 200) {
        return this.$message.error('编辑商品信息失败！')
      }
      // console.log(res.data)
      // this.editForm = res.data
      this.getGoodsList()
      this.$message.success('编辑商品信息成功！')
    },
    //  根据id删除商品
    async removeGoodById(id) {
      // console.log(id)
      const confirmResult = await this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 如果用户确认删除，则返回值为字符串 confirm
      // 如果用户取消了删除，则返回值为字符串 cancel
      // console.log(confirmResult)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      //  确定删除指定的操作
      const { data: res } = await this.$http.delete(`goods/${id}`)
      if (res.meta.status !== 200) {
        return this.$message.error('删除商品失败！')
      }
      // console.log(res.data)
      this.getGoodsList()
    },
    //  监听页数改变函数
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    //  处理页数发生改变函数
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    }
  }
}
</script>
<style lang="less" scoped>
</style>
